package services

import (
	"demo/models"
	"demo/pkg/database"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func GetAllNotices(c *gin.Context) {
	user := getUserInfo(c)
	resp := models.GetDefaultResp()

	// 调用数据库层查询电费账单
	notices, err := database.GetElectricitynoticesByUserId(user.Id)
	if err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}

	resp.Data = notices
	c.JSON(http.StatusOK, resp)
}



func UpadateNoticestatus(c *gin.Context) {
	newNotice := new(models.Notifications)
	resp := models.GetDefaultResp()
	if err := c.ShouldBindJSON(&newNotice); err != nil {
		setApiError(c, http.StatusInternalServerError, resp, err)
		return
	}
	notice, err := database.GetNoticeByNoticeId(newNotice.Id)
	if err != nil {
		logrus.Error("failed to get notice", err)
		setApiError(c, http.StatusInternalServerError, resp, err) 
		return
	}
	notice.IsRead = 1
	err = database.UpdateNoticeStatus(notice) 
	if err != nil {
		logrus.Error("failed to update notice", err)
		setApiError(c, http.StatusInternalServerError, resp, err) 
		return
	}
	resp.Data = notice 
	c.JSON(http.StatusOK, resp)
}